// app.js
const express = require('express')
const session = require('express-session')
const createError = require('http-errors')
const FileStore = require('session-file-store')(session)
const app = express()

app.use(session({
    secret: '12345-67890-09876-54321',
    resave: false,
    saveUninitialized: false,
    cookie: { secure: false, maxAge: 800000, httpOnly: false },
    name: 'session-id',
    store: new FileStore()
}))
app.use(express.json())
app.use(express.urlencoded({ extended: true }))



app.get('/', (req, res)=> {
    res.end('welcome to my homepage')
})


app.use('/user', require('./router/userRouter'))

app.use((req,res,next)=> {
    if (req.session.status) next()
    else next(createError(401))
})

app.use(require('./router/strictRouter'))



app.use((req, res, next) => {
    next(createError(404))
})

app.use((err, req, res,next)=> {
    res.status(err.status || 500)
    res.end(err.toString())
})

app.listen(3000)